#include "wifidbwindow.h"
#include "ui_wifidbwindow.h"
#include "initdb.h"

#include <QtSql>

wifiDBWindow::wifiDBWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::wifiDBWindow)
{
    ui->setupUi(this);
/*
    // check what drivers have
    QStringList driver = QSqlDatabase::drivers();
    foreach(QString d, driver)
        qDebug()<<d;
*/
    if (!QSqlDatabase::drivers().contains("QSQLITE"))
        QMessageBox::critical(this, "Unable to load database", "This demo needs the SQLITE driver");

    // initialize the database
    QSqlError err = initDb();

    if (QSqlError::NoError != err.type())
    {
        showError(err);
        return;
    }

    // Create the data model
    model = new QSqlRelationalTableModel(ui->wifiTable);
    model->setEditStrategy(QSqlTableModel::OnRowChange);
    model->setTable("aps");

    // Remember the indexes of the columns
    crackIdx = model->fieldIndex("cracker");
    //genreIdx = model->fieldIndex("clients");

    // Set the relations to the other database tables
    //model->setRelation(authorIdx, QSqlRelation("authors", "id", "name"));
    //model->setRelation(genreIdx, QSqlRelation("genres", "id", "name"));

    // Set the localized header captions
    //model->setHeaderData(authorIdx, Qt::Horizontal, tr("Author Name"));
    //model->setHeaderData(genreIdx, Qt::Horizontal, tr("Genre"));
    model->setHeaderData(model->fieldIndex("essid"), Qt::Horizontal, tr("ESSID(MAC)"));
    model->setHeaderData(model->fieldIndex("bssid"), Qt::Horizontal, tr("BSSID"));
    model->setHeaderData(model->fieldIndex("auth"), Qt::Horizontal, tr("AUTH"));
    model->setHeaderData(model->fieldIndex("power"), Qt::Horizontal, tr("POWER"));

    // Populate the model
    if (!model->select()) {
        showError(model->lastError());
        return;
    }

    // Set the model and hide the ID column
    ui->wifiTable->setModel(model);
    //ui.wifiTable->setItemDelegate(new BookDelegate(ui.wifiTable));
    //ui.wifiTable->setColumnHidden(model->fieldIndex("id"), true);
    //ui.wifiTable->setSelectionMode(QAbstractItemView::SingleSelection);

    // Initialize the crack edits
    //ui->passwdEdit->setModel(model->relationModel(crackIdx));
    //ui->passwdEdit->setText(model->relationModel(crackIdx)->fieldIndex("password"));

    //ui.genreEdit->setModel(model->relationModel(genreIdx));
    //ui.genreEdit->setModelColumn(model->relationModel(genreIdx)->fieldIndex("name"));

    QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
    mapper->setModel(model);
    //mapper->setItemDelegate(new BookDelegate(this));
    mapper->addMapping(ui->bssidEdit, model->fieldIndex("bssid"));
    mapper->addMapping(ui->essidEdit, model->fieldIndex("essid"));
    mapper->addMapping(ui->authEdit, model->fieldIndex("auth"));
    mapper->addMapping(ui->powerEdit, model->fieldIndex("power"));

    connect(ui->wifiTable->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
            mapper, SLOT(setCurrentModelIndex(QModelIndex)));
    /*
    connect(ui->essidEdit, SIGNAL(textChanged(QString)),
            this, SLOT(crackDisplay(QString )));
    connect(ui->essidEdit, SIGNAL(textChanged(QString)),
            this, SLOT(clientsDisplay(QString )));
*/
    connect(mapper, SIGNAL(currentIndexChanged(int)),
            this, SLOT(crackDisplay( )));
    connect(mapper, SIGNAL(currentIndexChanged(int)),
            this, SLOT(clientsDisplay( )));

    ui->wifiTable->setCurrentIndex(model->index(0, 0));

    return;

}

wifiDBWindow::~wifiDBWindow()
{
    delete ui;
}

void wifiDBWindow::showError(const QSqlError &err)
{
    QMessageBox::critical(this, "Unable to initialize Database",
                "Error initializing database: " + err.text());
}

void wifiDBWindow::on_addButton_clicked()
{
    QString mac = ui->essidEdit->text();
    QString bssid = ui->bssidEdit->text();
    QString auth = ui->authEdit->text();
    int power = ui->powerEdit->text().toInt();
    QVariant crackIdx;

    QSqlError err;

    if (ui->YesRadio->isChecked())
    {
        QString pwd = ui->passwdEdit->text();
        QString net = ui->netEdit->text();
        QString pin = ui->pinEdit->text();
        QString gw = ui->gwEdit->text();
        crackIdx = addCrack(mac, pwd, pin, net, gw);

    }

     err = addAps(mac, bssid, auth, power, crackIdx);
    if (QSqlError::NoError != err.type())
    {
        showError(err);
        return;
    }
    // Populate the model
    if (!model->select()) {
        showError(model->lastError());
        return;
    }
}

void wifiDBWindow::on_delButton_clicked()
{
    QString mac = ui->essidEdit->text();;

    QSqlError err = delAps(mac);
    if (QSqlError::NoError != err.type())
    {
        showError(err);
        return;
    }
    // Populate the model
    if (!model->select()) {
        showError(model->lastError());
        return;
    }
}

void wifiDBWindow::on_updateButton_clicked()
{
    QString mac = ui->essidEdit->text();
    QString bssid = ui->bssidEdit->text();
    QString auth = ui->authEdit->text();
    //int power = ui->powerEdit->text().toInt();
    QSqlError err;


    err = delAps(mac);
    if (QSqlError::NoError != err.type())
    {
        showError(err);
        return;
    }
    // Populate the model
    if (!model->select()) {
        showError(model->lastError());
        return;
    }
}

void wifiDBWindow::crackDisplay(QString mac)
{
    QMessageBox::critical(this, "mac in crackDisplay", mac );
}

void wifiDBWindow::clientsDisplay(QString mac)
{
    QMessageBox::critical(this, "mac in clientsDisplay", mac);
}

void wifiDBWindow::crackDisplay()
{
    QMessageBox::critical(this, "mac in crackDisplay" , ui->essidEdit->text());
}

void wifiDBWindow::clientsDisplay()
{
    QMessageBox::critical(this, "mac in clientsDisplay" , ui->essidEdit->text());
}
